package com.sofwin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.sofwin.pojo.Person;
import com.sofwin.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController extends BaseController {
    @Autowired
    private PersonService personService;

    @GetMapping("list")
    public AjaxResult list(){
        List<Person> list = personService.list();
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Person person){
        LambdaQueryWrapper<Person> wrapper = new LambdaQueryWrapper<>();
        if(person.getPId()==null&& StringUtils.isEmpty(personService.getById(person.getPId()).toString())){
            personService.save(person);
            return AjaxResult.success("新增成功");
        }
        personService.updateById(person);
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("del")
    public AjaxResult del(@RequestBody List<Integer> ids){
        personService.removeByIds(ids);
        return AjaxResult.success("批量删除成功");
    }
}
